package com.connector.base.message.model;

import com.alibaba.fastjson.JSONObject;
import com.connector.base.connection.Connection;
import com.connector.base.protocol.Command;
import com.connector.base.protocol.Packet;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public final class HandshakeMessage extends JsonBodyMessage
{
    private static final Logger log;
    private String userId;
    private String type;
    private String contentType;
    private String receiver;
    private String body;
    private String deviceId;
    private String osName;
    private String osVersion;
    private String clientVersion;
    private String mobile;
    private long timestamp;
    public int minHeartbeat;
    public int maxHeartbeat;
    public String appid;
    public String cid;

    public HandshakeMessage(final Connection connection) {
        super(new Packet(Command.HANDSHAKE.cmd, (byte)0, genSessionId()), connection);
    }

    public HandshakeMessage(final Packet packet, final Connection connection) {
        super(packet, connection);
    }

    @Override
    public void jsonToFields(final JSONObject jsonBody) {
        this.userId = (jsonBody.containsKey((Object)"userId") ? jsonBody.getString("userId") : null);
        this.type = (jsonBody.containsKey((Object)"type") ? jsonBody.getString("type") : null);
        this.contentType = (jsonBody.containsKey((Object)"contentType") ? jsonBody.getString("contentType") : null);
        this.receiver = (jsonBody.containsKey((Object)"receiver") ? jsonBody.getString("receiver") : null);
        this.body = (jsonBody.containsKey((Object)"body") ? jsonBody.getString("body") : null);
        this.deviceId = (jsonBody.containsKey((Object)"deviceId") ? jsonBody.getString("deviceId") : null);
        this.osName = (jsonBody.containsKey((Object)"osName") ? jsonBody.getString("osName") : null);
        this.osVersion = (jsonBody.containsKey((Object)"osVersion") ? jsonBody.getString("osVersion") : null);
        this.clientVersion = (jsonBody.containsKey((Object)"clientVersion") ? jsonBody.getString("clientVersion") : null);
        this.mobile = (jsonBody.containsKey((Object)"mobile") ? jsonBody.getString("mobile") : "1");
        this.timestamp = System.currentTimeMillis();
        this.minHeartbeat = (jsonBody.containsKey((Object)"minHeartbeat") ? jsonBody.getInteger("minHeartbeat") : 0);
        this.maxHeartbeat = (jsonBody.containsKey((Object)"maxHeartbeat") ? jsonBody.getInteger("maxHeartbeat") : 0);
        this.appid = (jsonBody.containsKey((Object)"appId") ? jsonBody.getString("appId") : null);
        this.cid = (jsonBody.containsKey((Object)"cid") ? jsonBody.getString("cid") : null);
    }

    @Override
    public JSONObject fieldToJson() {
        final JSONObject resultObj = new JSONObject();
        resultObj.put("userId", (Object)this.userId);
        resultObj.put("type", (Object)this.type);
        resultObj.put("contentType", (Object)this.contentType);
        resultObj.put("receiver", (Object)this.receiver);
        resultObj.put("body", (Object)this.body);
        resultObj.put("appid", (Object)this.appid);
        resultObj.put("cid", (Object)this.cid);
        resultObj.put("deviceId", (Object)this.deviceId);
        resultObj.put("osName", (Object)this.osName);
        resultObj.put("osVersion", (Object)this.osVersion);
        resultObj.put("clientVersion", (Object)this.clientVersion);
        resultObj.put("mobile", (Object)this.mobile);
        resultObj.put("timestamp", (Object)this.timestamp);
        resultObj.put("minHeartbeat", (Object)this.minHeartbeat);
        resultObj.put("maxHeartbeat", (Object)this.maxHeartbeat);
        return resultObj;
    }

    public static HandshakeMessage buildResponse(final BaseMessage src) {
        HandshakeMessage response = null;
        if (0 == src.packet.getType()) {
            response = new HandshakeMessage(new Packet(Command.HANDSHAKE.cmd, (byte)1, src.packet.getSessionId()), src.connection);
        }
        else if (2 == src.packet.getType()) {
            response = new HandshakeMessage(new Packet(Command.HANDSHAKE.cmd, (byte)4, src.packet.getSessionId()), src.connection);
        }
        return response;
    }

    protected static int genSessionId() {
        return HandshakeMessage.ID_SEQ.incrementAndGet();
    }

    public String getUserId() {
        return this.userId;
    }

    public String getType() {
        return this.type;
    }

    public String getContentType() {
        return this.contentType;
    }

    public String getReceiver() {
        return this.receiver;
    }

    public String getBody() {
        return this.body;
    }

    public String getDeviceId() {
        return this.deviceId;
    }

    public String getOsName() {
        return this.osName;
    }

    public String getOsVersion() {
        return this.osVersion;
    }

    public String getClientVersion() {
        return this.clientVersion;
    }

    public String getMobile() {
        return this.mobile;
    }

    public long getTimestamp() {
        return this.timestamp;
    }

    public int getMinHeartbeat() {
        return this.minHeartbeat;
    }

    public int getMaxHeartbeat() {
        return this.maxHeartbeat;
    }

    public String getAppid() {
        return this.appid;
    }

    public String getCid() {
        return this.cid;
    }

    public void setUserId(final String userId) {
        this.userId = userId;
    }

    public void setType(final String type) {
        this.type = type;
    }

    public void setContentType(final String contentType) {
        this.contentType = contentType;
    }

    public void setReceiver(final String receiver) {
        this.receiver = receiver;
    }

    public void setBody(final String body) {
        this.body = body;
    }

    public void setDeviceId(final String deviceId) {
        this.deviceId = deviceId;
    }

    public void setOsName(final String osName) {
        this.osName = osName;
    }

    public void setOsVersion(final String osVersion) {
        this.osVersion = osVersion;
    }

    public void setClientVersion(final String clientVersion) {
        this.clientVersion = clientVersion;
    }

    public void setMobile(final String mobile) {
        this.mobile = mobile;
    }

    public void setTimestamp(final long timestamp) {
        this.timestamp = timestamp;
    }

    public void setMinHeartbeat(final int minHeartbeat) {
        this.minHeartbeat = minHeartbeat;
    }

    public void setMaxHeartbeat(final int maxHeartbeat) {
        this.maxHeartbeat = maxHeartbeat;
    }

    public void setAppid(final String appid) {
        this.appid = appid;
    }

    public void setCid(final String cid) {
        this.cid = cid;
    }

    @Override
    public String toString() {
        return "HandshakeMessage(userId=" + this.getUserId() + ", type=" + this.getType() + ", contentType=" + this.getContentType() + ", receiver=" + this.getReceiver() + ", body=" + this.getBody() + ", deviceId=" + this.getDeviceId() + ", osName=" + this.getOsName() + ", osVersion=" + this.getOsVersion() + ", clientVersion=" + this.getClientVersion() + ", mobile=" + this.getMobile() + ", timestamp=" + this.getTimestamp() + ", minHeartbeat=" + this.getMinHeartbeat() + ", maxHeartbeat=" + this.getMaxHeartbeat() + ", appid=" + this.getAppid() + ", cid=" + this.getCid() + ")";
    }

    public HandshakeMessage() {
    }

    static {
        log = LoggerFactory.getLogger((Class) HandshakeMessage.class);
    }
}
